<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 我的个人数据
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">			
            <div class="handle-box">
                <el-button
                    type="primary"
                    icon="el-icon-delete"
                    class="handle-del mr10"
                    @click=""
                >批量删除</el-button>
                <el-select v-model="query.type" placeholder="状态" class="handle-select mr10">
                    <el-option key="1" label="收入" value="1"></el-option>
                    <el-option key="2" label="支出" value="2"></el-option>
                </el-select>
                <el-input v-model="query.name" placeholder="用户名" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
				<el-table-column label="类型" align="center">
				    <template slot-scope="scope">
				        <el-tag
				            :type="scope.row.type===1?'success':'danger'"
				        >{{scope.row.type===1?'收入':'支出'}}</el-tag>
				    </template>
				</el-table-column>
                <el-table-column prop="amount" label="数值"></el-table-column>
                <el-table-column prop="content" label="来源"></el-table-column>
				<el-table-column label="时间">
				    <template slot-scope="scope">{{scope.row.time|timeas}}</template>
				</el-table-column>
				<el-table-column label="状态" align="center">
				    <template slot-scope="scope">
				        <el-tag
				            :type="scope.row.state===1?'success':'danger'"
				        >{{scope.row.state===1?'正常':'锁定'}}</el-tag>
				    </template>
				</el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="身份认证" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="用户名">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="密码">
                    <el-input v-model="form.pass"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import api from '../../api/index';
import md5 from 'js-md5';
import tool from '../../utils/tools';

export default {
    name: 'ic',
    data() {
        return {
            query: {
                type: '',
                name: '',
                pageIndex: 1,
                pageSize: 10
            },
			auth: 1,
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {
				name: "",
				pass: ""
			},
            idx: -1,
            id: -1
        };
    },
    created() {
        // this.getData();
    },
	filters:{
		timeas(val){
			if(val=='') return ''
			return tool.stamp2datetime(val,1000).substring(0,10)
		}
	},
    methods: {
		getData() {
			if(this.auth===1){
				api.iclist(this.query).then(res => {
				    this.tableData = res.list;
					if(res.pageTotal){
						this.pageTotal = res.pageTotal || res.list.length;
					}
				});
			}else{
				console.error('您还没进行身份认证');
			}
		},
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form = row;
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            this.editVisible = false;
			if(md5(this.form.name+'-'+this.form.pass) == '31d28a724b5b760454962699fa18e9d2'){
				this.auth = 1;
				this.getData();
				this.$message.success(`认证成功`);
			}else{
				this.$message.error(`认证失败`);
			}
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        },
		// 触发搜索按钮
		handleSearch() {
		    this.$set(this.query, 'pageIndex', 1);
		    this.getData();
		},
		// 多选操作
		handleSelectionChange(val) {
		    this.multipleSelection = val;
		},
    },mounted(){
		if(this.auth === 0){
			this.editVisible = true;
		}
	}
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
